/*
 * @Author: 蔡霸霸
 * @Date: 2020-06-01 15:46:15
 * @LastEditors: 蔡霸霸
 * @LastEditTime: 2020-06-05 10:45:21
 * @FilePath: \Nuxt_music\store\actions\music.js
 */

export const music = {
  setMusciList({ commit, state }, data) {
    //* 当设置音乐列表
    const list = state.music_list
    if (data.length) {
      //* 保存列表歌曲
      if (list[0].name) {
        const a = data.concat(list)
        commit('Set_music_list', a)
      } else {
        commit('Set_music_list', data)
      }
    } else if (Boolean(list[0].name) === false) {
      commit('Set_music_list', [data])
    } else {
      for (const key in list) {
        if (list[key].id === data.id) {
          list.splice(key, 1)
          break
        }
      }
      list.unshift(data)
      commit('Set_music_list', list)
    }
    console.log(state.music_list)
  },
  delMusic({ commit, state }, url) {
    //* 删除列表中音乐
    const list = state.music_list
    for (const key in list) {
      if (list[key].source === url) {
        list.splice(key, 1)
        break
      }
      if (list.length === 0) {
        const obj = {
          name: '',
          artist: '',
          cover: '',
          source: '',
          id: ''
        }
        commit('Set_music_list', [obj])
      } else {
        commit('Set_music_list', list)
      }
    }
    console.log(list)
  },
  listSwitch({ commit, state }, id) {
    //* 弹窗列表内切换歌曲
    const list = state.music_list
    let arr = []
    for (const key in list) {
      if (list[key].id === id) {
        arr = list[key]
        list.splice(key, 1)
        break
      }
    }
    list.unshift(arr)
    commit('Set_music_list', list)
  }
}
